package com.spzx.auth.service;

import com.spzx.auth.form.RegisterBody;
import com.spzx.common.core.constant.Constants;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.ip.IpUtils;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.system.api.model.LoginUser;
import com.spzx.user.api.RemoteUserInfoService;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class H5TokenService {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

//    @Autowired
//    RemoteUserInfoService remoteUserInfoService;
   @Resource
   RemoteUserInfoService remoteUserInfoService;
    @Autowired
    SysRecordLogService sysRecordLogService;

    @Autowired
    SysPasswordService passwordService;

    public void register(RegisterBody registerBody) {
        String phoneName = registerBody.getUsername();
        String code = registerBody.getCode();
        String nickName = registerBody.getNickName();
        String password = registerBody.getPassword();
        // 验证码
//        String codeCache = (String) redisTemplate.opsForValue().get("phone:code:" + phoneName);
        String codeCache = (String) redisTemplate.opsForValue().get("phone:code:"+phoneName);
        Assert.isTrue(code.equals(codeCache), "验证码错误");

        // 远程注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phoneName);
        userInfo.setNickName(nickName);
        userInfo.setPassword(password);
        remoteUserInfoService.register(userInfo, SecurityConstants.INNER);

        // 保存
        sysRecordLogService.recordLogininfor(phoneName, Constants.REGISTER, "注册成功");// 保存前台系统业务日志
    }

    public LoginUser login(String username, String password) {

//        String passwordSecurity = SecurityUtils.encryptPassword(password);

        // 根据username查询用户信息
        R<UserInfo> userInfoR = remoteUserInfoService.getUserInfo(username, SecurityConstants.INNER);
        // int code = userInfoR.getCode();
        UserInfo userInfoDb = userInfoR.getData();
        Assert.notNull(userInfoDb,"用户不存在");

        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(userInfoDb,loginUser);
        loginUser.setUserid(userInfoDb.getId());
        passwordService.validate(loginUser, password);
//         Assert.isTrue(passwordSecurity.equals(userInfoDb.getPassword()), "密码错误");

        // 记录登录ip
        UpdateUserLogin updateUserLogin = new UpdateUserLogin();
        updateUserLogin.setUserId(userInfoDb.getId());
        updateUserLogin.setLastLoginIp(IpUtils.getIpAddr());
        updateUserLogin.setLastLoginTime(new Date());
        remoteUserInfoService.updateUserLogin(updateUserLogin,SecurityConstants.INNER);


        return loginUser;
    }

}
